package com.qg.contriller;
import com.alibaba.fastjson.JSONObject;
import com.qg.utils.Constants;
import com.qg.dto.Dto;
import com.qg.module.vo.QgUserVo;
import com.qg.utils.KafKaUtil;
import com.qg.module.pojo.QgUser;
import com.qg.service.use_token.TokenService;
import com.qg.service.user_login.LoginQgUser;
import com.qg.utils.DtoUtil;
import com.qg.utils.EmptyUtils;
import com.qg.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/api")
public class UserLoginController {

    @Resource
    LoginQgUser loginQgUser;

    @Resource
    RedisUtil redis;
    @Resource
    TokenService tokenService;
    @Resource
    KafKaUtil kafKaUtil;

    /**
     * 用户登录
     * @param phon
     * @param password
     * @return
     */
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public Dto userLogin(@RequestParam(value = "phone",required = true)String phon,@RequestParam(value = "password",required = true)String password) throws Exception{



            QgUser qgUser = null;
            String tokenValue = null;
            String tokenKey = null;
            //获取用户
            qgUser = loginQgUser.queryUser_PhoneAndPassword(phon, password);

            //登录失败
            if (EmptyUtils.isEmpty(qgUser)) {
                kafKaUtil.sendInfoMessage("登录失败！");
                return DtoUtil.returnFail("用户和密码出错！", Constants.USER_USERANDPASS_ERROR);

            } else {//登录成功
                //日志消息
                kafKaUtil.sendInfoMessage("登录成功!");

                //======判断是否有token有则延续无则生成
                tokenKey=tokenService.validateToken(qgUser.getId());
                    //无token
                if (EmptyUtils.isEmpty(tokenKey)) {
                    //==========生成token并储存redis
                    try {
                        //生成token
                        tokenKey = tokenService.getToken("PC", qgUser);
                        //储存token
                        redis.setStr(qgUser.getId(), tokenKey,Constants.TOKEN_TIMEOUT);
                        tokenService.saveToken(tokenKey, qgUser);
                    } catch (Exception e) {
                        kafKaUtil.sendErrorMessage(e);
                        return DtoUtil.returnFail("保存token出错！", Constants.TOKEN_ERROR);
                    }
                }

                //==============返回用户和token信息
                QgUserVo qgUserVo = new QgUserVo();
                BeanUtils.copyProperties(qgUser, qgUserVo);
                qgUserVo.setToken(tokenKey);

                //返回用户信息和token到客户端
                return DtoUtil.returnDataSuccess(qgUserVo);
            }


    }


    /**
     * 用户退出
     * @param token
     * @return
     */
    @RequestMapping(value = "/v/loginOut",method = RequestMethod.POST)
    public Dto outLogin(@RequestParam(value = "token",required = true)String token)throws Exception{
        //=========判断token是否过期
        String tokenValue=tokenService.validateToken(token);
        QgUser qgUser= JSONObject.parseObject(tokenValue,QgUser.class);
        if (EmptyUtils.isNotEmpty(tokenValue)) {
            //执行退出
            tokenService.deleteToken(token);
            tokenService.deleteToken(qgUser.getId());

            //如果有其他业务数据，也需要销毁，未完待续
            //。。。。。。。。
            return DtoUtil.returnSuccess();
        } else {
            //错误状态，
            return DtoUtil.returnFail("Token无效", Constants.TOKEN_ERROR);

        }
    }

    }
